package jsyf.oa.service.intentCustomerFromBusiness;

import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.intentCustomerFromBusiness.IntentCustomerFromBusinessDao;
import jsyf.oa.facade.intentCustomerFromBusiness.IntentCustomerFromBusinessFacade;
import jsyf.oa.service.report.reportCommon.ReportCommonMethodsAndConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by 92852 on 2018/9/8.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class IntentCustomerFromBusinessService implements IntentCustomerFromBusinessFacade {
    @Autowired
    private IntentCustomerFromBusinessDao intentCustomerFromBusinessDao;

    @Autowired
    private ReportCommonMethodsAndConstant common;

    @Override
    public String save(String jsonData) {
        return null;
    }

    /*获取个人*/
    @Override
    public JSONObject getForPersonalIntentCustomerList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String key = jsonParams.getString("key");

        StringBuffer condition = new StringBuffer();
        if (StringUtils.isNotBlank(userId) && StringUtils.isNotEmpty(userId)) {
            condition.append("AND t_rpt_thursday_genzong.REGIONAL_MANAGER_ID='"+userId+"'\n");
        }
        if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(key)) {
            condition.append("AND( t_rpt_thursday_genzong.CUSTOMER_NAME LIKE '%"+key+"%'\n" +
                    "OR t_rpt_thursday_genzong.CUSTOMER_TEL LIKE '%"+key+"%'\n" +
                    "OR t_rpt_thursday_genzong.NEED_MODEL LIKE '%"+key+"%'\n" +
                    "OR t_rpt_thursday_genzong.BUY_TYPE LIKE '%"+key+"%'\n" +
                    ")\n");
        }
        /*分页信息*/
        int requestPage = 1;
        int pageSize = Integer.MAX_VALUE;
        if (jsonParams.containsKey("pageSize")) {
            pageSize = jsonParams.getInteger("pageSize");
        }
        if (jsonParams.containsKey("requestPage")) {
            requestPage = jsonParams.getInteger("requestPage");
        }
        List<JSONObject> records = intentCustomerFromBusinessDao.getForPersonalIntentCustomerList(condition.toString(), requestPage, pageSize);
        int recordCount = intentCustomerFromBusinessDao.getCountPersonalIntentCustomerList(userId, condition.toString());
        //如果整除表示正好分N页，如果不能整除在N页的基础上+1页
        int pageCount = recordCount % pageSize == 0 ? recordCount / pageSize : (recordCount / pageSize) + 1;
        //存放分页信息
        JSONObject sendData = new JSONObject();
        //存放list
        JSONObject result = new JSONObject();
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        // 子数组对象
        result.put("records", records);
        return result;
    }


    @Override
    public List<JSONObject> getForDepartIntentCustomerList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        return intentCustomerFromBusinessDao.getForDepartIntentCustomerList(userId);
    }



    /*集团*/
    @Override
    public List<JSONObject> getForGroupIntentCustomerList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String companyId = common.getAllCompanysIdByUserId(userId);
        return intentCustomerFromBusinessDao.getForGroupIntentCustomerList(companyId);
    }

    @Override
    public JSONObject getAllReportCustomer(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String key = jsonParams.getString("key");
        StringBuffer condition = new StringBuffer();
        if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(key)) {
            condition.append("AND (t_wrt_customer.`NAME` LIKE '%"+key+"%' OR  t_wrt_customer.`TEL` LIKE '%"+key+"%'  )");
        }
        //分页信息
        int requestPage = 1;
        int pageSize = Integer.MAX_VALUE;
        if (jsonParams.containsKey("pageSize")) {
            pageSize = jsonParams.getInteger("pageSize");
        }
        if (jsonParams.containsKey("requestPage")) {
            requestPage = jsonParams.getInteger("requestPage");
        }
        /*获取身份标志*/
        JSONObject roleFlag = intentCustomerFromBusinessDao.getManageFlagAndFrontFlagAndAreaTypeByUserId(userId);
        /*获取集团人员可以看到的公司*/
        String companyId = common.getAllCompanysIdByUserId(userId);
        /*获取所有意向客户*/
        List<JSONObject> records = intentCustomerFromBusinessDao.getAllIntentCustomerList(userId,condition.toString(),companyId, requestPage, pageSize, roleFlag);
        /*获得查询数目*/
        int recordCount = intentCustomerFromBusinessDao.getCountByCompanyIdAndUserIdAndKey(companyId, userId, condition.toString(), roleFlag);

        //分装前端需要的格式
        int pageCount = 0;
        if (recordCount < pageSize) {
            pageCount = 1;
        } else {
            double flag = Math.ceil(recordCount / pageSize + 1);
            pageCount = ((int) flag);
        }
        if (recordCount == 0) {
            pageCount = 0;
        }
        //存放分页信息
        JSONObject sendData = new JSONObject();
        //存放list
        JSONObject result = new JSONObject();
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        // 子数组对象
        result.put("records", records);
        return result;
    }

    /*集团页面点击公司*/
    @Override
    public List<JSONObject> getForDepartByCompanyId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String companyId = jsonParams.getString("companyId");
        return intentCustomerFromBusinessDao.getForDepartByCompanyId(companyId);
    }




    @Override
    public List<JSONObject> getForCompanyIntentCustomerList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        if (StringUtils.isNotBlank(userId) && StringUtils.isNotEmpty(userId)) {
              /*获取人员可以看到的公司*/
            String companyId = common.getAllCompanysIdByUserId(userId);
            return intentCustomerFromBusinessDao.getForCompanyIntentCustomerList(companyId);
        }else {
            return null;
        }

    }



}
